package com.boot;

import ch.qos.logback.core.db.DBHelper;
import com.boot.bean.Cat;
import com.boot.bean.Pet;
import com.boot.bean.User;
import com.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;

import javax.jws.soap.SOAPBinding;
import java.util.Map;


@ImportResource("classpath:bean.xml")    // 导入资源
@SpringBootApplication(scanBasePackages = "com")  // 扫描包基础结构目录
public class MainApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

//        String[] beanDefinitionNames = run.getBeanDefinitionNames();
//        for (String beanDefinitionName : beanDefinitionNames) {
//             System.out.println(beanDefinitionName);
//        }
//        // 从容其中获取组件
//        Pet tom01 = run.getBean("tom", Pet.class);
//        Pet tom02 = run.getBean("tom", Pet.class);
//        // 默认都是单例
//        System.out.println("tom组件："+(tom01 == tom02));
//
//        // 配置类也是组件
//        //  com.boot.config.MyConfig$$EnhancerBySpringCGLIB$$7a73b07@f559c74
//        MyConfig bean = run.getBean(MyConfig.class);
//        System.out.println(bean);
//
//        // @Configuration(proxyBeanMethods = true) 代理对象调用方法，
//        // SpringBoot总会检查这个组件是否存在容器中，保证组件单实例
//        User user1 = bean.getUser();
//        User user2 = bean.getUser();
//        System.out.println(user1 == user2);
//
//
//        User user = run.getBean("getUser", User.class);
//
//        Pet pet = run.getBean("tom", Pet.class);
//
//        System.out.println("用户的宠物："+(user.getPet().equals(pet)));
//
//
//        System.out.println("---------------------");
//
//        String[] userTypes = run.getBeanNamesForType(User.class);
//
//        for (String userType : userTypes) {
//            System.out.println(userType);
//        }
        Map<String, Cat> beansOfType = run.getBeansOfType(Cat.class);
        beansOfType.forEach((k, v) -> {
            System.out.println(k + "******" + v);
        });
//        System.out.println("---------------------------------");
//        DBHelper dbHelper = run.getBean(DBHelper.class);
//        System.out.println(dbHelper);


        // 容器中是否有这个tom组件
        boolean tom = run.containsBean("tom");

        // 容器中是否有这个getUser组件
        boolean getUser = run.containsBean("getUser");
        System.out.println(getUser);


        // 获取bean.xml 资源
        Cat catbean = run.getBean("catbean", Cat.class);
        System.out.println(catbean);

        // 获取cat绑定yml配置
        //Cat cat = run.getBean("cat", Cat.class);
        Cat cat = run.getBean("mycat-com.boot.bean.Cat", Cat.class);
        System.out.println(cat);
    }
} 
